package com.web.frame.mysql;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.sql.*;

@Component
public class UserLoginRegister {
    Logger logger = LoggerFactory.getLogger(UserLoginRegister.class);
    public String SQL_ULR = "jdbc:mysql://localhost:3306/web";
    public String USER_NAME = "root";
    public String PASSWORD = "PASSWORD";
    public String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    public String SQL_COMMAND_FIND_USER = "SELECT name,password FROM user where name=? and password=?";
    public String SQL_COMMAND_FIND_USER_REGISTER = "SELECT name FROM user where name=?";
    public String SQL_COMMAND_CREATE_USER = "INSERT INTO user (name,password) values(?,?)";
    public String SQL_COMMAND_DELETE_USER = "DELETE FROM user WHERE name=?";

    public Connection connection = null;
    public ResultSet resultSet = null;
    public PreparedStatement preparedStatement = null;
    public String login(String userName, String userPassword){
        try {
            Class.forName(JDBC_DRIVER);
            connection = DriverManager.getConnection(SQL_ULR, USER_NAME, PASSWORD);
            preparedStatement = connection.prepareStatement(SQL_COMMAND_FIND_USER);
            preparedStatement.setString(1, userName);
            preparedStatement.setString(2, userPassword);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                resultSet.close();
                preparedStatement.close();
                connection.close();
                if ("Admin".equals(userName)){
                    logger.info("管理员登录成功!");
                }else {
                    logger.info("登录成功!用户名:" + userName + ",密码:" + userPassword);
                }
                return "success";
            }else {
                resultSet.close();
                preparedStatement.close();
                connection.close();
                if ("Admin".equals(userName)){
                    logger.info("管理员登录不成功,疑似冒充!");
                }else {
                    logger.info("登录不成功!用户名:" + userName + ",密码:" + userPassword);
                }
                return "noSuccess";
            }
        }catch (ClassNotFoundException e){
            logger.info("登录错误!错误代码:" + e);
            return "error";
        }catch (SQLException e){
            logger.info("登录错误!错误代码:" + e);
            return "error";
        }
    }
    public String register(String userName, String userPassword){
        try {
            Class.forName(JDBC_DRIVER);
            connection = DriverManager.getConnection(SQL_ULR, USER_NAME, PASSWORD);
            preparedStatement = connection.prepareStatement(SQL_COMMAND_FIND_USER_REGISTER);
            preparedStatement.setString(1, userName);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                preparedStatement.close();
                resultSet.close();
                connection.close();
                logger.info("创建用户失败!因为已经有同名用户:" + userName);
                return "noSuccess";
            }else {
                preparedStatement = connection.prepareStatement(SQL_COMMAND_CREATE_USER);
                preparedStatement.setString(1, userName);
                preparedStatement.setString(2, userPassword);
                preparedStatement.execute();
                preparedStatement.close();
                resultSet.close();
                connection.close();
                if ("Admin".equals(userName)){
                    logger.info("管理员初始化注册成功!");
                }else {
                    logger.info("创建用户成功!用户名:" + userName + ",密码:" + userPassword);
                }
                return "success";
            }
        }catch (ClassNotFoundException e){
            logger.info("创建用户错误!错误代码:" + e);
            return "error";
        }catch (SQLException e){
            logger.info("创建用户错误!错误代码:" + e);
            return "error";
        }
    }
    /*
    public String deleteUser(String userName){
        logger.info(userName + "尝试删除用户");
        if ("Admin".equals(userName)){
            logger.info("管理员用户无法删除!");
            return "noSuccess";
        }else {
            try {
                Class.forName(JDBC_DRIVER);
                connection = DriverManager.getConnection(SQL_ULR, USER_NAME, PASSWORD);
                preparedStatement = connection.prepareStatement(SQL_COMMAND_DELETE_USER);
                preparedStatement.setString(1, userName);
                preparedStatement.addBatch();
                preparedStatement.executeBatch();
                preparedStatement.close();
                connection.close();
                logger.info("已删除用户" + userName + "的账户");
                return "success";
            }catch (ClassNotFoundException e){
                logger.info("删除用户错误!错误代码:" + e);
                return "error";
            }catch (SQLException e) {
                logger.info("删除用户错误!错误代码:" + e);
                return "error";
            }
        }
    }
     */
}
